<template>
  <!-- 导航部分 -->
  <el-row style="margin: 10px;background-color: ghostwhite;padding: 15px">
    <el-col :span="24">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>首页</el-breadcrumb-item>
        <el-breadcrumb-item>设备管理</el-breadcrumb-item>
        <el-breadcrumb-item>点检设备保养</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>
  </el-row>

  <!--搜索栏-->
  <el-row style="margin: 10px;background-color: ghostwhite;padding: 15px">
    <el-col :span="24">
      <el-form inline>
        <el-form-item label="项目编码">
          <el-input v-model="itemsQuery.projectCode"/>
        </el-form-item>
        <el-form-item label="项目名称">
          <el-input v-model="itemsQuery.projectName"/>
        </el-form-item>
        <el-form-item label="项目类型">
          <el-input v-model="itemsQuery.projectType"/>
        </el-form-item>
        <el-form-item>
          <el-button @click="loadItems" type="primary">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>

  <!-- 工具栏 -->
  <el-row style="margin: 20px">
    <el-col :span="24">
      <el-button type="primary" plain @click="toAdd"><el-icon><Plus /></el-icon>新增</el-button>
      <el-button type="success" plain @click="toUpdate"><el-icon><EditPen /></el-icon>编辑</el-button>
      <el-button type="warning" plain @click="toDeletes"><el-icon><EditPen /></el-icon>删除</el-button>
    </el-col>
  </el-row>


<!--  &lt;!&ndash; 工具栏 &ndash;&gt;-->
<!--  <el-row style="margin: 10px">-->
<!--    <el-col :span="24">-->
<!--      <el-button type="primary" @click="toAdd">新增</el-button>-->
<!--      <el-button type="primary" @click="toUpdate">编辑</el-button>-->
<!--      <el-button type="primary" @click="toDeletes">删除</el-button>-->
<!--    </el-col>-->
<!--  </el-row>-->

  <!-- 数据列表 -->
  <el-row style="margin: 0px 10px;padding: 0px 15px">
    <el-col :span="24">
      <el-table :data="items" border stripe ref="itemsTable">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="projectCode" label="项目编码"/>
        <el-table-column prop="projectName" label="项目名称"/>
        <el-table-column prop="projectType" label="项目类型"/>
        <el-table-column prop="projectContent" label="项目内容"/>
        <el-table-column prop="projectStandard" label="标准"/>
        <el-table-column prop="projectStatus" label="是否启用">
          <template #default="scope">
            <el-tag type="info" v-if="scope.row.projectStatus == 1">是</el-tag>
            <el-tag type="warning" v-if="scope.row.projectStatus == 2">否</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="projectNotes" label="备注"/>
        <el-table-column label="操作" width="330">
          <template #default="scope">
            <el-link style="margin: 0px 5px" @click="toEdit(scope.row.projectId)" type="primary">
              <el-icon>
                <EditPen/>
              </el-icon>
              编辑
            </el-link>
            <el-link style="margin: 0px 5px" @click="toDelete(scope.row.projectId)" type="primary">
              <el-icon>
                <Delete/>
              </el-icon>
              删除
            </el-link>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
  </el-row>

  <el-row style="margin-top: 10px">
    <!-- 监听页码的变化事件 -->
    <el-pagination @change="changePage" :total="total" background layout="prev, pager, next"/>
  </el-row>

  <!-- 新增的弹窗 -->
  <el-dialog v-model="addMaintainItemsVisible"
             title="添加设备点检保养项目"
             width="50%"
  >
    <el-form :model="newItems">
      <el-form-item label="项目编码">
        <el-input v-model="newItems.projectCode"/>
      </el-form-item>
      <el-form-item label="项目名称">
        <el-input v-model="newItems.projectName"/>
      </el-form-item>
      <el-form-item label="项目类型">
        <el-radio-group v-model="newItems.projectType">
          <el-radio value="设备检点" size="large">设备检点</el-radio>
          <el-radio value="设备保养" size="large">设备保养</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="项目内容">
        <el-input v-model="newItems.projectContent"/>
      </el-form-item>
      <el-form-item label="项目标准">
        <el-input v-model="newItems.projectStandard"/>
      </el-form-item>
      <el-form-item  label="是否启用">
        <el-radio-group v-model="newItems.projectStatus">
          <el-radio value="1" size="large">是</el-radio>
          <el-radio value="2" size="large">否</el-radio>
        </el-radio-group>
      </el-form-item>
<!--      <el-form-item label="是否启用">-->
<!--        <el-select style="width: 100px" v-model="newItems.projectStatus">-->
<!--          <el-option :value="1" label="是"/>-->
<!--          <el-option :value="2" label="否"/>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="备注">
        <el-input v-model="newItems.projectNotes"/>
      </el-form-item>

    </el-form>
    <el-button @click="addMaintainItemsVisible =false" newLedger={}>取消</el-button>
    <el-button type="primary" @click="addItem">
      确定
    </el-button>
  </el-dialog>

  <!--编辑子组件-->
  <ItemEdit :projectId="updateProjectId" @editComplate="loadItems"/>


</template>

<script setup>
import {onMounted, ref} from "vue";
import {Delete, EditPen} from "@element-plus/icons-vue";
import maintainItemsRequest from "@/api/maintainItems.js";
import {ElMessage, ElMessageBox} from "element-plus";
import LedgerEdit from "@/views/machineType/machineLedgerEdit.vue";
import ItemEdit from "@/views/machineType/machineMaintainItemsEdit.vue"
import ledgerRequest from "@/api/ledger.js";

let itemsQuery = ref({});
let items = ref([]);
let curPage = ref(1);
let total = ref(0);
let itemsTable = ref([]);
let newItems =ref({});
let updateProjectId =ref(-1);
//行编辑
function toEdit(projectId) {
  updateProjectId.value = projectId;
}

//行删除
function toDelete(projectId) {
  ElMessageBox.confirm(
      '您确定要删除吗',
      'Warning',
      {
        confirmButtonClass:'是的',
        cancelButtonClass:'取消',
        type:'warning',
      }
  ).then(()=>{
    maintainItemsRequest.deleteItem(projectId,res=>{
      if (res.code == 1){
        ElMessage({
          type:"success",
          message:"操作完成"

        })
        loadItems();
      }else {
        ElMessage({
          type:"warning",
          message:"删除失败"
        })
      }
    })
    loadItems();
  })
}

//显示详情
function showDetail(projectId) {

}

let addMaintainItemsVisible = ref(false);
//工具栏新增
function toAdd() {
  addMaintainItemsVisible.value = true;
}
//工具栏新增提交按钮
function addItem(){
  maintainItemsRequest.addItems(newItems.value,res=>{
    if (res.code == 200){
      ElMessage({
        type:"success",
        message:"添加成功"
      })
    }else {
      ElMessage({
        type:"warning",
        message:"添加失败"
      })
    }
    newItems.value = {};
    addMaintainItemsVisible.value =false;
    loadItems();
  })
}

//工具栏编辑
function toUpdate() {
  let selectionRows = itemsTable.value.getSelectionRows();
  if (selectionRows.length > 1){
    ElMessage({
      type:"warning",
      message:"只能选择一条数据"
    })
  }else if(selectionRows.length == 0){
    ElMessage({
      type:"warning",
      message:"请选择数据"
    })
  }else{
    toEdit(selectionRows[0].projectId);
  }
}

//工具栏删除
function toDeletes() {
  let selectionRows = itemsTable.value.getSelectionRows();
  if (selectionRows.length == 0 ){
    ElMessage({
      type:"warning",
      message:"请选择数据"
    })
    return;
  }
  ElMessageBox.confirm(
      '您确定要删除吗？',
      'warning',
      {
        confirmButtonClass:'是的',
        cancelButtonClass:'取消',
        type:'warning',
      }
  ).then(()=>{
    selectionRows.forEach(row=>{
      maintainItemsRequest.deleteItem(row.projectId,res=>{
        console.log(row.projectId);
        if (res.code == 1){
          ElMessage({
            type:"success",
            message:"删除成功",
          })
        }else {
          ElMessage({
            type:"warning",
            message:"删除失败",
          })
        }
      })
    })
    loadItems();
  })
}

//搜索按钮
function loadItems() {
  maintainItemsRequest.loadItems(itemsQuery.value, curPage.value, 10, res => {
    total.value = res.data.total;
    items.value = res.data.list;
  })
}

function changePage(value) {
  curPage.value = value;
  loadItems();
}

onMounted(() => {
  loadItems();
})

</script>

<style scoped>

</style>